package donjuan;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class Message {
  public static Scanner in = new Scanner(System.in);
  
  static Boolean judgement = Boolean.valueOf(true);//判断是否建立新数据表
  
  static Boolean Link_start = Boolean.valueOf(false);//决定是否开始链接
  
  static ArrayList<Readin> read = new ArrayList<>(3);//读入的数据会被存储到该动态对象数组
  
  static Connection con = null;
  
  static Statement state = null;
  
  static PrepareFrame show;
  
  public static void main(String[] args) 
  {
	  try {
		    JFrame.setDefaultLookAndFeelDecorated(true);
		    UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
		} catch (Exception e) {
		    e.printStackTrace();
		}
	  SwingUtilities.invokeLater(Message::createWindow);
  }
  
  public static void registersql() {//通过接受用户的账户名和密码，来注册数据库
    try {
      Class.forName("com.mysql.cj.jdbc.Driver");
    } catch (Exception exception) {}
    String uri = "jdbc:mysql://localhost/chopin?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=GMT&characterEncoding=gb2312";
    //uri的localhost可以继续修改，chopin部分的数据库名称也可以修改
    try {
      con = DriverManager.getConnection(uri, Window_ready.contain[0], Window_ready.contain[1]);
    } catch (SQLException e) {
      e.printStackTrace();
    } 
  }
  public static void activatesql() {//该静态方法用于用户已经输入数据表名字之后使用
    if (judgement) {//真则为读取已经存在的表数据
      Readin.name = Window_ready.contain[2];
      try {
        state = con.createStatement();
        ResultSet rs = state.executeQuery("select * FROM " + Readin.name);
        while (rs.next()) {
          Readin.judge(read);
          read.add(new Readin(rs));
        } 
      } catch (SQLException e) {
        System.exit(-1);
      } 
    } else {//否则是注册新表
      Readin.name = Window_ready.contain[2];
      try {
        state = con.createStatement();
        String sqlstr = "create table " + Readin.name + 
          " (Id INT PRIMARY KEY AUTO_INCREMENT,Account varchar(225)," + 
          "Password varchar(225),Website varchar(225)," + 
          "Information varchar(225),Note varchar(225))DEFAULT CHARSET=gb2312;";
        state.executeUpdate(sqlstr);
      } catch (Exception e) {
      e.printStackTrace();
      } 
    } 
  }
  private static void createWindow() {
    show = new PrepareFrame();
  }
}
